/*
Copyright (C) 2007 Lahiru Lakmal Priyadarshana.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Will represent the dll object once loaded
var dll = null;
var isDocked, isMinimized;
var color = "#FFFFFF"; var key = "0"; var secolor = "#F70303";

function view_onOpen() {    
  try {
    // Attempt to load the MyStuff object from the dll
    dll = new MyStuff();
  } catch (e) {
    view.alert(e);
    return;
  }

  setInterval("update()",50);

  // Assume the gadget is docked at first.
  // The onDisplayTargetChange handler will be notified if it is in fact
  // undocked.
  isDocked = true;

  // Handle notifications sent when gadget is docked or undocked.
  pluginHelper.onDisplayTargetChange = onDisplayTargetChange;

  // Handle notifications sent when gadget is minimized. 
  view.onminimize = onMinimize;
  // Handle notifications sent when gadget is restored (maximized)
  view.onrestore = onRestore; 
}

function update() {
  // Call the MyYesNoCancelPrompt method that is compiled in the DLL. The
  // source code for this function is in the YesNoCancel directory.
  var result = dll.GetNumber(autocp.value);

  result = result.split("-");
  color = result[0];
  key = result[1];

  if(key=="1"){secolor = color;}

  reDraw();
}

function selected(color) {
  divselected.background = color;
  lblscolor.innerText = color;
}

function reDraw() {
  if (isMinimized) {
    // The gadget is minimized.
    // Write status to the caption.
    var caption = "Screen Picker : " + color + " : " + secolor; 
    // Set the caption (title bar).
    view.caption = caption;

    // Nothing else to do, return.
    return;
  } else {
    // The gadget is not minimized..	  
    // Set to normal caption.
    view.caption = strings.GADGET_NAME;
  }

  // Display the color
  lblcolor.innerText = color;
  divcolor.background = color;
  selected(secolor);

  // Special handling for when the gadget is docked or undocked.
  if (isDocked) {
    // Set background.
    imgbac.src = "images/bac_docked.PNG";
    imgbac.width = 137;
    imgbac.height = 123;
    
    // Set appropriate size.
    view.width = 137;
    view.height = 123;

    // Positioning color boxes.
    divcolor.width = 42; divcolor.height = 42;
    divselected.width = 71; divselected.height = 71;

    divcolor.x = 6; divcolor.y = 7;
    divselected.x = 58; divselected.y = 44;
    lblcolor.x = 36; lblcolor.y = 14;
    lblscolor.x = 6; lblscolor.y = 85;
    autocp.x = 22; autocp.y = 57;
   
  } else {
    // Set background.
    imgbac.src = "images/bac.PNG";
    imgbac.width = 250;
    imgbac.height = 150;
    
    // Set appropriate size.
    view.width = 250;
    view.height = 150;

    // Positioning color boxes.
    divcolor.width = 67; divcolor.height = 67;
    divselected.width = 86; divselected.height = 88;

    divcolor.x = 8; divcolor.y = 7;
    divselected.x = 155; divselected.y = 55;
    lblcolor.x = 57; lblcolor.y = 15;
    lblscolor.x = 101; lblscolor.y = 107;
    autocp.x = 164; autocp.y = 19;
  }
}

function onDisplayTargetChange(displayTarget) {
  // Find out the new display mode
  if (displayTarget == gddTargetSidebar) {
    debug.trace('Display target changed to docked');
    isDocked = true;
    reDraw();
  } else if (displayTarget == gddTargetFloatingView) {
    debug.trace('Display target changed to undocked');
    isDocked = false;
    reDraw();
  }
}

function onMinimize() {
  debug.trace('Minimized');
  isMinimized = true;
  reDraw();
}

function onRestore() {
  debug.trace('Restored');
  isMinimized = false;
  reDraw();
}